﻿#region itdc

/************************************************************************
*Copyright(C) 2015 by qsgy
 *All rights reserved. 
 *FileName:     $safeitemname$
 *Author:       oyh
 *Version:      
 *UnityVersion：
 *Date:   $time$      
 *Description:    
 *History: 
************************************************************************/
#endregion
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Hold : MonoBehaviour {
    #region 1
    public bool isHolding = false;
    public Collider workP;
    public Collider holder;
    public Animator ani;
    public bool tagHolding = false;//


    #endregion


    private void Update()
    {
        if (tagHolding!=isHolding)
        {
            if (tagHolding)
            {
                lock (this)
                {
                    GetHold();
                    tagHolding = isHolding;

                }
            }
            else
            {
                Release();
            }
        }
    }


    //抓取物块
    public void GetHold()
    {
        ani.SetBool("hold", true);
       // print("hold");
        if (workP!=null)
        {
            Invoke("HoldAct", 0.2f);
            isHolding = true;
            Isholding();

        }
        else
        {
            Invoke("ReleaseAni", 1f);
        }
            
    }
    void ReleaseAni()
    {
        ani.SetBool("hold", false);
    }

    private void HoldAct()
    {
        workP.GetComponent<WorkPiece>().isHolding = true;
        workP.GetComponent<Rigidbody>().useGravity = false;
        holder = workP;
        holder.GetComponent<Collider>().enabled = false;
        workP.transform.SetParent(transform);
        workP.transform.Translate(new Vector3(0, 0.01f, 0));//浮空不接触传送带
    }

    //释放抓住的物块
    public void Release()
    {
            ani.SetBool("hold", false);
        if (!isHolding) return;
        workP.GetComponent<Rigidbody>().useGravity = true;
        holder.GetComponent<WorkPiece>().isHolding = false;
        holder.GetComponent<Collider>().enabled = true;
        holder.transform.SetParent(null);
        holder = null;
        isHolding = false;
            Isholding();

    }




    //是否有物体在抓取范围
    public void CanHold()
    {
        // Debug.Log(workP != null);
        // Application.ExternalCall("CanHold", workP != null);
        ByteBuffer bf = ByteBuffer.Allocate(12);
        bf.WriteShort(10);
        bf.WriteUTF("canhold");
        bf.WriteBoolean(workP != null);
        print(bf.Capacity());
        SocketFace.instance.SendBuf(bf);

    }

    //是否为抓取状态
    public void Isholding()
    {
        //  Debug.Log(isHolding);
        // Application.ExternalCall("IsHolding", isHolding);
        ByteBuffer bf = ByteBuffer.Allocate(11);
        bf.WriteShort(9);
        bf.WriteUTF("ishold");
        bf.WriteBoolean(isHolding);
        SocketFace.instance.SendBuf(bf);
    }



    private void OnTriggerEnter(Collider other)
    {
        if (other.tag== "WorkPiece")
        {
            workP = other;
            Isholding();
            CanHold();
        }
    }

    private void OnTriggerExit(Collider other)
    {
        if (other==workP)
        {
            workP = null;
            Isholding();
            CanHold();
        }
    }
}
